package com.example.module.favorites.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.unit.ConvertUnit;
import com.example.common.unit.GetUserInfoBySessionUnit;
import com.example.module.favorites.DO.FavoritesAddDO;
import com.example.module.favorites.DO.GetFavoritesDO;
import com.example.module.favorites.form.FavoritesAddForm;
import com.example.module.favorites.form.FavoritesDeleteForm;
import com.example.module.favorites.form.GetFavoritesForm;
import com.example.module.favorites.mapper.FavoritesMapper;
import com.example.module.favorites.service.FavoritesService;
import com.example.module.favorites.vo.GetFavoritesVO;
import com.example.module.goods.form.GetGoodsForm;
import com.example.module.goods.mapper.GoodsMapper;
import com.example.module.goods.vo.GetGoodsVO;
import com.example.module.user.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ghy
 * @date 2025/08/15
 * @description 收藏夹实现类
 */
@Service
public class FavoritesImpl implements FavoritesService {
    @Resource
    private ConvertUnit convertUnit;
    @Resource
    private FavoritesMapper favoritesMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private GetUserInfoBySessionUnit getUserInfoBySessionUnit;

    /*
     * 添加收藏
     *
     * @param favoritesAddForm
     * @return void
     * */
    @Override
    public void addFavorites(FavoritesAddForm favoritesAddForm) {
        //获取添加收藏的信息
        Long userId = getUserInfoBySessionUnit.getUserId();
        Long goodsId = favoritesAddForm.getGoodsId();
        Date favoritesTime = new Date();

        //创建GetGoodsForm存goodsId并查询Id对应商品
        GetGoodsForm getGoodsForm = new GetGoodsForm();
        getGoodsForm.setGoodsId(goodsId);
        List<GetGoodsVO> goods = goodsMapper.getGoods(getGoodsForm);
        if (goods.size() == 0) {
            throw new RuntimeException("商品收藏失败：商品不存在");
        }

        // 检查是否已收藏该商品
        GetFavoritesForm checkForm = new GetFavoritesForm();
        checkForm.setUserId(userId);
        checkForm.setGoodsId(goodsId);
        List<GetFavoritesVO> existingFavorites = favoritesMapper.getFavorites(checkForm);
        if (existingFavorites.size() > 0) {
            throw new RuntimeException("商品收藏失败：商品已收藏");
        }
        //添加信息
        FavoritesAddDO favoritesAddDO = new FavoritesAddDO();
        favoritesAddDO.setUserId(userId);
        favoritesAddDO.setGoodsId(goodsId);
        favoritesAddDO.setFavoritesTime(favoritesTime);
        //添加到数据库
        favoritesMapper.addFavorites(favoritesAddDO);
    }


    /*
     * 删除收藏
     *
     * @param FavoritesDeleteForm
     * @return void
     */
    @Override
    public void deleteFavorites(FavoritesDeleteForm favoritesDeleteForm) {
        //判断收藏是否存在
        GetFavoritesForm getFavoritesForm = ConvertUnit.FormToDO(favoritesDeleteForm, GetFavoritesForm.class);
        List<GetFavoritesVO> favorites = favoritesMapper.getFavorites(getFavoritesForm);
        if (favorites.size() == 0) {
            throw new RuntimeException("删除收藏失败：收藏不存在");
        }
        //删除收藏
        favoritesMapper.deleteFavorites(favoritesDeleteForm.getFavoritesId());
    }

    /*
     * 查询收藏
     *
     * @param GetFavoritesForm
     * @return List<GetFavoritesDO>
     * */
    @Override
    public List<GetFavoritesVO> getFavorites(GetFavoritesForm getFavoritesForm) {
        //获取用户Id
        Long userId = getUserInfoBySessionUnit.getUserId();
        getFavoritesForm.setUserId(userId);
        //查询收藏
        List<GetFavoritesVO> result = favoritesMapper.getFavorites(getFavoritesForm);
        //返回结果
        return result;
    }

    /*
     * 分页查询收藏
     *
     * @param getFavoritesForm 查询条件
     * @return Page<GetFavoritesVO> 分页结果
     */
    @Override
    public Page<GetFavoritesVO> getFavoritesPage(GetFavoritesForm getFavoritesForm) {
        // 获取用户Id
        Long userId = getUserInfoBySessionUnit.getUserId();
        getFavoritesForm.setUserId(userId);
        // 设置分页参数
        long current = getFavoritesForm.getCurrent() != null ? getFavoritesForm.getCurrent() : 1;
        long size = getFavoritesForm.getSize() != null ? getFavoritesForm.getSize() : 10;
        // 构造分页对象
        Page<GetFavoritesVO> page = new Page<>(current, size);
        // 执行分页查询
        Page<GetFavoritesVO> result = favoritesMapper.getFavoritesByPage(page, getFavoritesForm);
        return result;
    }
}
